﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using System.Runtime.InteropServices;

public class AI : MonoBehaviour
{

    // Start is called before the first frame update
    public GameObject target;//目标
    private NavMeshAgent agent;
    public EnemyState CurrentState = EnemyState.search;//设置敌人初始状态
    private GameObject[] experience;
    private GameObject[] blood;
    private GameObject[] character;
    private float AttackTime = 0;
    private float refreshTime = 0.3f;
    SortedList<float, GameObject> exfg = new SortedList<float, GameObject>();
    SortedList<float, GameObject> blfg = new SortedList<float, GameObject>();
    SortedList<float, GameObject> chafg = new SortedList<float, GameObject>();


    public delegate void tempAt();

    public enum EnemyState
    {
        search,
        run,
        attack,
        angry,
        death
    }

    void Start()
    {

        agent = GetComponent<NavMeshAgent>();
        CurrentState = EnemyState.search;


        GameObject[] character1 = GameObject.FindGameObjectsWithTag("Player");
        GameObject[] character2 = GameObject.FindGameObjectsWithTag("Enemy");
        character = new GameObject[character1.Length + character2.Length];
        character1.CopyTo(character, 0);
        character2.CopyTo(character, character1.Length);
    }


    // Update is called once per frame
    void Update()
    {
        //refreshTime += Time.deltaTime;
        //if (refreshTime > 0.3f)
        //{
        experience = GameObject.FindGameObjectsWithTag("EmpiricalBall");
        blood = GameObject.FindGameObjectsWithTag("Blood");
      
        refreshTime = 0;
        //}

        //判断血量
        if (gameObject.GetComponent<Enemy>().hp < 5)
        {
            CurrentState = EnemyState.run;//有一个bug当地图上没血包时会卡住
        }
        else
        {
            //判断是否攻击

            if (SearchCharaRencently() != null)
            {
                CurrentState = EnemyState.attack;
            }
            else
            {
                CurrentState = EnemyState.search;
            }

        }
        switch (CurrentState)
        {
            //开始状态
            case EnemyState.search:
                GameObject tempEx = SearchExperienceRencently();
                // agent.SetDestination(tempEx.transform.position);
                if (tempEx != null)
                {
                    agent.stoppingDistance = 0;
                    agent.destination = tempEx.transform.position;
                    Vector3 temp1 = new Vector3(tempEx.transform.position.x, 0, tempEx.transform.position.z);
                    gameObject.transform.LookAt(temp1);
                }
                else
                {
                    SearchExperienceRencently();
                }
                break;
            //逃跑
            case EnemyState.run:
                GameObject tempBl = SearchBloodRencently();
                if (tempBl == null)
                {
                    CurrentState = EnemyState.search;
                }
                else
                {
                    agent.stoppingDistance = 0;
                    agent.SetDestination(tempBl.transform.position);
                    Vector3 temp2 = new Vector3(tempBl.transform.position.x, 0, tempBl.transform.position.z);
                    gameObject.transform.LookAt(temp2);
                }
                break;
            //攻击
            case EnemyState.attack:
                GameObject tempcha = SearchCharaRencently();
                agent.stoppingDistance = 3.5f;
                agent.SetDestination(tempcha.transform.position);
                Vector3 temp3 = new Vector3(tempcha.transform.position.x, 0, tempcha.transform.position.z);
                gameObject.transform.LookAt(temp3);
                AttackTime += Time.deltaTime;
                if (AttackTime > 1f)
                {
                    Attack();
                    AttackTime = 0;
                }
                break;
            case EnemyState.death:
                agent.isStopped = true;
                break;

        }

    }

    /// <summary>
    /// 搜索最近的经验球
    /// </summary>
    public GameObject SearchExperienceRencently()
    {
        exfg.Clear();
        foreach (GameObject i in experience)
        {
            if (i != null)
            {
                float temp = Vector3.Distance(i.transform.position, transform.position);
                if (!exfg.ContainsKey(temp))
                {
                    exfg.Add(temp, i);
                }
            }

        }
        return exfg.Values[0];


    }
    /// <summary>
    /// 搜索最近的血球
    /// </summary>
    public GameObject SearchBloodRencently()
    {
        blfg.Clear();
        foreach (GameObject i in blood)
        {
            if (i != null)
            {
                float temp = Vector3.Distance(i.transform.position, transform.position);
                if (blfg.ContainsKey(temp))
                {
                }
                else
                {
                    blfg.Add(temp, i);
                }
            }

        }
        if (blfg.Count > 0)
        {
            return blfg.Values[0];
        }
        else
        {
            return null;
        }


    }
    /// <summary>
    /// 搜索最近的玩家
    /// </summary>
    public GameObject SearchCharaRencently()
    {


        foreach (GameObject i in character)
        {
            if (i != null)
            {
                float temp = Vector3.Distance(i.transform.position, transform.position);
                if (chafg.ContainsKey(temp))
                {
                }
                else
                {
                    chafg.Add(temp, i);
                }
            }

        }
        if (chafg.Keys[1] == null)
        {
            return null;
        }
        if (chafg.Keys[1] < 9)
        {
            return chafg.Values[1];
        }
        else
        {
            return null;
        }
    }


    public void Attack()
    {
        gameObject.GetComponent<Enemy>().Attack();
    }



}


